package cn.dzhy.myzf.qq.email.api;

import cn.dzhy.myzf.qq.email.model.Group;
import cn.dzhy.myzf.qq.email.model.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.token.TokenManager;

/**
 * @package cn.dzhy.myzf.qq.email.api
 * @Date Created in 2018/3/15 下午10:58
 * @Author myzf
 */
public class GroupApi extends EmailApi {

    private final TokenManager tokenManager;
    public GroupApi(TokenManager tokenManager){
        this.tokenManager = tokenManager;
    }

    /**
     * 创建邮件群组
     * @see__https://exmail.qq.com/qy_mng_logic/doc#10022
     * @className GroupApi
     * @author myzf(yangzhuangfei_min@odao.com)
     * @date 2018/3/15 下午11:04
     * @since JDK 1.8
     * @see cn.dzhy.myzf.qq.email.api
     * @return 
     * @throws WeixinException
     */
    public ApiResult createGroup(Group group) throws WeixinException {
        String group_create_uri = getRequestUri("group_create_uri");
        Token token = tokenManager.getCache();
        JSONObject obj = (JSONObject) JSON.toJSON(group);
        WeixinResponse response = weixinExecutor.post(String.format(group_create_uri, token.getAccessToken()), obj.toJSONString());
        return response.getAsResult();
    }

    /**
     * 更新邮件群组
     * @see__https://exmail.qq.com/qy_mng_logic/doc#10023
     * @className GroupApi
     * @author myzf(yangzhuangfei_min@odao.com)
     * @date 2018/3/15 下午11:04
     * @since JDK 1.8
     * @see cn.dzhy.myzf.qq.email.api
     * @return
     * @throws WeixinException
     */
    public ApiResult updateGroup(Group group) throws WeixinException {
        String group_update_uri = getRequestUri("group_update_uri");
        Token token = tokenManager.getCache();
        JSONObject obj = (JSONObject) JSON.toJSON(group);
        WeixinResponse response = weixinExecutor.post(String.format(group_update_uri, token.getAccessToken()), obj.toJSONString());
        return response.getAsResult();
    }

    /**
     * 删除邮件群组
     * @see__https://exmail.qq.com/qy_mng_logic/doc#10024
     * @className GroupApi
     * @author myzf(yangzhuangfei_min@odao.com)
     * @date 2018/3/17 下午4:55
     * @since JDK 1.8
     * @see cn.dzhy.myzf.qq.email.api
     * @return 
     * @throws WeixinException
     */
    public ApiResult deleteGroup(String groupId) throws WeixinException {
        String group_delete_uri = getRequestUri("group_del_uri");
        Token token = tokenManager.getCache();
        WeixinResponse response = weixinExecutor.get(String.format(group_delete_uri, token.getAccessToken(), groupId));
        return response.getAsResult();
    }

    /**
     * 获取邮件群组信息  返回的department 有点问题
     * @see__https://exmail.qq.com/qy_mng_logic/doc#10025
     * @className GroupApi
     * @author myzf(yangzhuangfei_min@odao.com)
     * @date 2018/3/17 下午4:21
     * @since JDK 1.8
     * @see cn.dzhy.myzf.qq.email.api
     * @return 
     * @throws WeixinException
     */
    public Group getGroup(String groupid) throws WeixinException {
        String group_create_uri = getRequestUri("group_get_uri");
        Token token = tokenManager.getCache();
        WeixinResponse response = weixinExecutor.get(String.format(
                group_create_uri, token.getAccessToken(), groupid));
        JSONObject obj = response.getAsJson();
        Group group = JSON.toJavaObject(obj, Group.class);
        return group;
    }
    
}
